package com.mccken.practice.top100.Top_1$20.Top_23;

import com.mccken.practice.top100.ListNode;
import com.mccken.practice.top100.ListNodeUtil;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-01-28 19:30
 **/
public class HasCycle {

	public boolean hasCycle(ListNode head) {

		if (head == null || head.next == null) {
			return false;
		}

		ListNode slow = head;
		ListNode fast = head;

		while (fast.next != null && fast.next.next != null) {
			fast = fast.next.next;
			slow = slow.next;

			if (fast == slow) {
				return true;
			}
		}

		return false;

	}

	public static void main(String[] args) {
		int[] arr = {3,2,0,-4};
		ListNode head = ListNodeUtil.buildListNode(arr);
		HasCycle hasCycle = new HasCycle();
		System.out.println(hasCycle.hasCycle(head));
		ListNodeUtil.printListNode(head);
	}
	
}
